
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <title>GreenDao使用 · GitBook</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="GreenDao源码分析.html" />
    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="GreenDao使用.html">
            
                <a href="GreenDao使用.html">
            
                    
                    GreenDao使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="GreenDao源码分析.html">
            
                <a href="GreenDao源码分析.html">
            
                    
                    GreenDao源码分析
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >GreenDao使用</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>GreenDao3.0&#x4F7F;&#x7528;</p><h4 id="1&#x6784;&#x5EFA;">1.&#x6784;&#x5EFA;</h4><pre><code>buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath &apos;org.greenrobot:greendao-gradle-plugin:3.2.1&apos;
    }
}

apply plugin: &apos;org.greenrobot.greendao&apos;

dependencies {
    compile &apos;org.greenrobot:greendao:3.2.0&apos;
}</code></pre>
<h4 id="2&#x63D2;&#x4EF6;&#x914D;&#x7F6E;">2.&#x63D2;&#x4EF6;&#x914D;&#x7F6E;</h4><pre><code>greendao {
    targetGenDir &apos;src/main/java&apos;
}</code></pre>
<ul><li>schemaVersion&#xFF1A; &#x6570;&#x636E;&#x5E93;schema&#x7248;&#x672C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x6570;&#x636E;&#x5E93;&#x7248;&#x672C;&#x53F7;
</li>
<li>daoPackage&#xFF1A;&#x8BBE;&#x7F6E;DaoMaster &#x3001;DaoSession&#x3001;Dao&#x5305;&#x540D;
</li>
<li>targetGenDir&#xFF1A;&#x8BBE;&#x7F6E;DaoMaster &#x3001;DaoSession&#x3001;Dao&#x76EE;&#x5F55;
</li>
<li>targetGenDirTest&#xFF1A;&#x8BBE;&#x7F6E;&#x751F;&#x6210;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;
</li>
<li>generateTests&#xFF1A;&#x8BBE;&#x7F6E;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;
</li></ul>
<h4 id="3&#x521B;&#x5EFA;&#x8868;">3.&#x521B;&#x5EFA;&#x8868;</h4><pre><code class="lang-java"><span class="hljs-meta">@Entity</span>()
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> </span>{
    <span class="hljs-meta">@Id</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">long</span> no;
    <span class="hljs-meta">@NotNull</span> <span class="hljs-meta">@Unique</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;
    <span class="hljs-meta">@Property</span>(nameInDb = <span class="hljs-string">&quot;studentName&quot;</span>) <span class="hljs-meta">@Unique</span> <span class="hljs-keyword">private</span> String name;
    <span class="hljs-meta">@Transient</span> <span class="hljs-keyword">private</span> String nickname;

    <span class="hljs-meta">@Generated</span>(hash = <span class="hljs-number">773426453</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Student</span><span class="hljs-params">(<span class="hljs-keyword">long</span> no, <span class="hljs-keyword">int</span> age, String name)</span> </span>{
        <span class="hljs-keyword">this</span>.no = no;
        <span class="hljs-keyword">this</span>.age = age;
        <span class="hljs-keyword">this</span>.name = name;
    }

    <span class="hljs-meta">@Generated</span>(hash = <span class="hljs-number">1556870573</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Student</span><span class="hljs-params">()</span> </span>{
    }


    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">long</span> <span class="hljs-title">getNo</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> no;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNo</span><span class="hljs-params">(<span class="hljs-keyword">long</span> no)</span> </span>{
        <span class="hljs-keyword">this</span>.no = no;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAge</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> age;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAge</span><span class="hljs-params">(<span class="hljs-keyword">int</span> age)</span> </span>{
        <span class="hljs-keyword">this</span>.age = age;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> name;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>{
        <span class="hljs-keyword">this</span>.name = name;
    }


    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getNickname</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> nickname;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNickname</span><span class="hljs-params">(String nickname)</span> </span>{
        <span class="hljs-keyword">this</span>.nickname = nickname;
    }

}</code></pre>
<p>@Entity&#x6CE8;&#x89E3;</p><ul><li>schema&#xFF1A;&#x544A;&#x77E5;GreenDao&#x5F53;&#x524D;&#x5B9E;&#x4F53;&#x5C5E;&#x4E8E;&#x54EA;&#x4E2A;schema
</li>
<li>active&#xFF1A;&#x6807;&#x8BB0;&#x4E00;&#x4E2A;&#x5B9E;&#x4F53;&#x5904;&#x4E8E;&#x6D3B;&#x52A8;&#x72B6;&#x6001;&#xFF0C;&#x6D3B;&#x52A8;&#x5B9E;&#x4F53;&#x6709;&#x66F4;&#x65B0;&#x3001;&#x5220;&#x9664;&#x548C;&#x5237;&#x65B0;&#x65B9;&#x6CD5;
</li>
<li>nameInDb&#xFF1A;&#x5728;&#x6570;&#x636E;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x522B;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x5B9E;&#x4F53;&#x7684;&#x7C7B;&#x540D;
</li>
<li>indexes&#xFF1A;&#x5B9A;&#x4E49;&#x7D22;&#x5F15;&#xFF0C;&#x53EF;&#x4EE5;&#x8DE8;&#x8D8A;&#x591A;&#x4E2A;&#x5217;
</li>
<li>createInDb&#xFF1A;&#x6807;&#x8BB0;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x8868;
</li></ul>
<p>&#x5C5E;&#x6027;&#x6CE8;&#x89E3;</p><ul><li>@Id :&#x4E3B;&#x952E; Long&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;@Id(autoincrement = true)&#x8BBE;&#x7F6E;&#x81EA;&#x589E;&#x957F;
</li>
<li>@Property&#xFF1A;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x975E;&#x9ED8;&#x8BA4;&#x5173;&#x7CFB;&#x6620;&#x5C04;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x5217;&#x540D;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;&#x7684;&#x4F7F;&#x7528;&#x5B57;&#x6BB5;&#x540D; &#x4E3E;&#x4F8B;&#xFF1A;@Property (nameInDb=&quot;name&quot;)
</li>
<li><p>@Transient &#xFF1A;&#x6DFB;&#x52A0;&#x6B21;&#x6807;&#x8BB0;&#x4E4B;&#x540E;&#x4E0D;&#x4F1A;&#x751F;&#x6210;&#x6570;&#x636E;&#x5E93;&#x8868;&#x7684;&#x5217;
&#x7EA6;&#x675F;&#x6761;&#x4EF6;</p></li>
<li><p>@NotNul&#xFF1A;&#x8BBE;&#x7F6E;&#x6570;&#x636E;&#x5E93;&#x8868;&#x5F53;&#x524D;&#x5217;&#x4E0D;&#x80FD;&#x4E3A;&#x7A7A;</p></li>
<li><p>@Unique &#xFF1A;&#x8BBE;&#x7F6E;&#x552F;&#x4E00;&#x7EA6;&#x675F;</p></li></ul>
<p>&#x751F;&#x6210;StudentDao&#x7684;</p><pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">createTable</span><span class="hljs-params">(Database db, <span class="hljs-keyword">boolean</span> ifNotExists)</span> </span>{
    String constraint = ifNotExists? <span class="hljs-string">&quot;IF NOT EXISTS &quot;</span>: <span class="hljs-string">&quot;&quot;</span>;
    db.execSQL(<span class="hljs-string">&quot;CREATE TABLE &quot;</span> + constraint + <span class="hljs-string">&quot;\&quot;STUDENT\&quot; (&quot;</span> + <span class="hljs-comment">//</span>
            <span class="hljs-string">&quot;\&quot;_id\&quot; INTEGER PRIMARY KEY NOT NULL ,&quot;</span> + <span class="hljs-comment">// 0: no</span>
            <span class="hljs-string">&quot;\&quot;AGE\&quot; INTEGER NOT NULL UNIQUE ,&quot;</span> + <span class="hljs-comment">// 1: age</span>
            <span class="hljs-string">&quot;\&quot;studentName\&quot; TEXT UNIQUE );&quot;</span>); <span class="hljs-comment">// 2: name</span>
}</code></pre>
<h4 id="&#x63D2;&#x5165;">&#x63D2;&#x5165;</h4><pre><code class="lang-java">Student student = <span class="hljs-keyword">new</span> Student(<span class="hljs-number">0</span>,<span class="hljs-number">20</span>,<span class="hljs-string">&quot;test0&quot;</span>);
DBManager.getStudentDao(<span class="hljs-keyword">this</span>).insert(student);<span class="hljs-comment">//&#x5355;&#x4E2A;&#x63D2;&#x5165;</span>
List&lt;Student&gt; students = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    Student s = <span class="hljs-keyword">new</span> Student(i+<span class="hljs-number">1</span>,i+<span class="hljs-number">20</span>,<span class="hljs-string">&quot;test&quot;</span>+(i+i));
    students.add(s);
}
DBManager.getStudentDao(<span class="hljs-keyword">this</span>).insertInTx(students);<span class="hljs-comment">//&#x6279;&#x91CF;&#x63D2;&#x5165;</span></code></pre>
<h4 id="&#x7F3A;&#x70B9;">&#x7F3A;&#x70B9;</h4><ul><li>&#x4E0D;&#x652F;&#x6301;<a href="https://github.com/greenrobot/greenDAO/issues/449" target="_blank">ON CONFLICT</a>
</li></ul>
<h4 id="&#x53C2;&#x8003;">&#x53C2;&#x8003;</h4><ul><li><a href="http://greenrobot.org/greendao/documentation/" target="_blank">&#x5B98;&#x65B9;&#x6587;&#x6863;</a>
</li></ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: Introduction">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="GreenDao源码分析.html" class="navigation navigation-next " aria-label="Next page: GreenDao源码分析">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"GreenDao使用","level":"1.2","depth":1,"next":{"title":"GreenDao源码分析","level":"1.3","depth":1,"path":"GreenDao源码分析.md","ref":"GreenDao源码分析.md","articles":[]},"previous":{"title":"Introduction","level":"1.1","depth":1,"path":"README.md","ref":"README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"GreenDao使用.md","mtime":"2016-12-20T08:03:45.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-12-20T06:34:16.286Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

